var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
import { getCurrentInstance, mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
import { mountComponent, usePopupState } from "../utils";
import FanToast from "./toast.js";
const defaultOptions = {
  message: "",
  icon: "none",
  duration: 2e3,
  delay: 0,
  mask: true
};
let queue;
function parseOptions(message) {
  if (typeof message === "object")
    return message;
  return {
    message: message ? String(message) : ""
  };
}
function createInstance() {
  const {
    instance
  } = mountComponent({
    setup() {
      const {
        open,
        state,
        close,
        toggle
      } = usePopupState();
      const render = () => {
        return _createVNode(FanToast, _mergeProps(state, {
          "onUpdate:show": toggle
        }), null);
      };
      getCurrentInstance().render = render;
      return {
        open,
        close
      };
    }
  });
  return instance;
}
function getInstance() {
  if (!queue) {
    const instance = createInstance();
    queue = instance;
  }
  return queue;
}
const create = (options, o = {
  icon: "none"
}) => {
  clear();
  const toast2 = getInstance();
  const parsedOptions = parseOptions(options);
  toast2.open(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), parsedOptions), o));
  return toast2;
};
const toast = (options) => create(options);
toast.success = (options) => {
  create(options, {
    icon: "success"
  });
};
toast.error = (options) => {
  create(options, {
    icon: "error"
  });
};
toast.loading = (options) => {
  create(options, {
    icon: "loading",
    duration: 0
  });
};
const clear = () => __async(void 0, null, function* () {
  if (queue) {
    queue.close();
    queue = null;
  }
});
toast.clear = clear;
export {
  toast
};
